package com.shinewind.search.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.pojo.doc.ArticleDoc;
import com.shinewind.pojo.entity.Article;
import com.shinewind.pojo.entity.Model;
import com.shinewind.pojo.entity.News;
import com.shinewind.pojo.vo.ArticleVo;
import com.shinewind.search.mapper.IArticleMapper;
import com.shinewind.search.service.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl extends ServiceImpl<IArticleMapper, Article> implements IArticleService {
    @Autowired
    private INewsService iNewsService;
    @Autowired
    private IModelService iModelService;
    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private IUserDetailService iUserDetailService;

    @Override
    public Article getArticleById(String mId) {
        Article article = this.lambdaQuery().eq(Article::getMId,mId).one();
        return article;
    }

    @Override
    public ArticleVo getArticleByMid(String mId) {
        //查询文章相关信息
        Article article = this.getArticleById(mId);
        ArticleVo articleVo = ArticleVo.builder().build();
        BeanUtils.copyProperties(article,articleVo);
        //查询相关商品或新闻
        Optional<News> news = Optional.ofNullable(iNewsService.getNewsInfoById(article.getMId()));
        if(ObjectUtil.isNotNull(news) && "1".equals(news.get().getIsModel())){
            Model model = iModelService.getModelByMId(mId);
            articleVo.setName(model.getTitle());
            articleVo.setSrc(model.getHref());
            articleVo.setIsModel(news.get().getIsModel());
        }
        else if(ObjectUtil.isNotNull(news) && "0".equals(news.get().getIsModel())){
            articleVo.setName(news.get().getTitle());
            articleVo.setSrc(news.get().getHref());
            articleVo.setIsModel(news.get().getIsModel());
        }
        return articleVo;
    }

    @Override
    public List<ArticleDoc> getAllArticleDoc() {
        List<Article> articles = this.lambdaQuery().list();
        List<ArticleDoc> articleDocs = articles.stream().map(article -> {
            ArticleDoc articleDoc = ArticleDoc.builder().build();
            BeanUtils.copyProperties(article,articleDoc);
            //处理content内容,只返回文字
            String content = article.getContent();
            //获取content中img的src属性
            Document document = Jsoup.parse(content);
            Elements imgs = document.getElementsByTag("img");
            List<String> imagesUrl = new ArrayList<>();
            for(Element img : imgs){
                if(StrUtil.isNotBlank(img.attr("src")))
                    imagesUrl.add(img.attr("src"));
            }
            articleDoc.setImages(imagesUrl);
            //去除html标签
            content = HtmlUtil.cleanHtmlTag(article.getContent());
            //将转义字符还原
            content = HtmlUtil.unescape(content);
            articleDoc.setContent(content);
            //查询用户昵称和头像
            articleDoc.setNickName(iUserInfoService.getUserById(article.getUId()).getNickName());
            articleDoc.setAvatar(iUserDetailService.getUserDetailById(article.getUId()).getAvatar());
            //设置关联的模型或新闻编号
            articleDoc.setMId(article.getMId());
            //是否评测的是模型
            //查询相关商品或新闻
            Optional<News> news = Optional.ofNullable(iNewsService.getNewsInfoById(article.getMId()));
            if(ObjectUtil.isNotNull(news) && "1".equals(news.get().getIsModel())){
                Model model = iModelService.getModelByMId(article.getMId());
                articleDoc.setIsModel(news.get().getIsModel());
                articleDoc.setSrc(model.getHref());
                articleDoc.setName(model.getGoodsName());
            }
            else if(ObjectUtil.isNotNull(news) && "0".equals(news.get().getIsModel())){
                articleDoc.setIsModel(news.get().getIsModel());
                articleDoc.setSrc(news.get().getHref());
                articleDoc.setName(news.get().getTitle());
            }
            return articleDoc;
        }).collect(Collectors.toList());
        return articleDocs;
    }

    @Override
    public Article getArticleByAid(Integer aId) {
        Article article = this.lambdaQuery().eq(Article::getAId,aId).one();
        return article;
    }
}
